/*
 * Copyright (c) 2012,2014 Apple Inc. All Rights Reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 */


#include "utilities/der_plist.h"
#include "utilities/der_plist_internal.h"

#include "utilities/SecCFRelease.h"
#include "utilities/array_size.h"

#include <CoreFoundation/CFData.h>

#include "utilities_regressions.h"

#define kMaxResultSize 512

struct test_case {
    size_t      data_size;
    uint8_t     data[kMaxResultSize];
    size_t      result_size;
    uint8_t     result[kMaxResultSize];
};

static struct test_case test_cases[] = {
    { .data_size = 3,   .data = { 0xFE, 0xFB, 0x10 },
      .result_size = 5, .result = { 0x04, 0x03, 0xFE, 0xFB, 0x10, }, },
    { .data_size = 260,   .data = { 0xf2, 0x82, 0x8f, 0x71, 0x50, 0xdc, 0x2e, 0x7d, 0x1d, 0x9e, 0x1c, 0xea,
                                    0xb4, 0x47, 0x2c, 0xc7, 0xc6, 0x11, 0x5d, 0x2d, 0xe6, 0xec, 0x3d, 0xea,
                                    0x24, 0x06, 0x6a, 0x37, 0xbc, 0x80, 0xdd, 0x8e, 0x9b, 0xf0, 0x5d, 0x5c,
                                    0x1a, 0xb4, 0x48, 0xec, 0x23, 0x20, 0x98, 0x63, 0x87, 0x0e, 0x21, 0xa6,
                                    0x20, 0xef, 0xd8, 0xde, 0xd8, 0x89, 0xca, 0x8c, 0x42, 0x3c, 0xce, 0xdc,
                                    0xc3, 0x7f, 0xa0, 0xb2, 0xce, 0xf6, 0xcc, 0x0b, 0x46, 0x5f, 0xc0, 0xd6,
                                    0x2a, 0x6e, 0x0d, 0x4e, 0x78, 0xa1, 0x4c, 0x17, 0xb6, 0xa7, 0x48, 0xca,
                                    0x05, 0x54, 0xf1, 0xa1, 0x03, 0xac, 0xb0, 0x2e, 0xa5, 0xdd, 0xf2, 0x6a,
                                    0x51, 0xe5, 0xa0, 0xcf, 0xd1, 0x94, 0xbc, 0x8c, 0x45, 0xa8, 0xa6, 0x41,
                                    0x71, 0xaf, 0x9c, 0x95, 0xb3, 0xb6, 0x71, 0x07, 0xdb, 0x95, 0xc9, 0x36,
                                    0x18, 0xbc, 0x61, 0x24, 0x33, 0x4c, 0x39, 0x04, 0x82, 0xcb, 0x1d, 0x09,
                                    0xf4, 0xb7, 0x2c, 0xe8, 0x23, 0x5e, 0x24, 0x99, 0x85, 0x0a, 0xc4, 0x2f,
                                    0xe7, 0xdc, 0xb4, 0x3c, 0xa8, 0xf3, 0xfb, 0xc6, 0x6e, 0xd8, 0xfd, 0x76,
                                    0xc2, 0x50, 0x15, 0xc9, 0x6d, 0x2f, 0xcf, 0x1b, 0x8f, 0x73, 0x24, 0x19,
                                    0x12, 0x21, 0xa1, 0x50, 0x37, 0x8f, 0xa4, 0x27, 0x6c, 0x0b, 0x39, 0xef,
                                    0x24, 0xd8, 0x3d, 0x97, 0xc8, 0x46, 0x5d, 0x86, 0x0f, 0x99, 0x24, 0x48,
                                    0x94, 0xd0, 0x51, 0x49, 0x9a, 0xb2, 0x29, 0x51, 0x66, 0x41, 0xd0, 0x52,
                                    0xa3, 0x15, 0x5a, 0x99, 0xfb, 0xf2, 0x2d, 0xfc, 0x73, 0xa7, 0x03, 0xd5,
                                    0xb6, 0x45, 0x48, 0x11, 0x93, 0xa9, 0xb7, 0x2d, 0xd4, 0x22, 0xbb, 0x87,
                                    0xa3, 0xd4, 0x5d, 0xff, 0xda, 0x84, 0x91, 0x6c, 0xea, 0x72, 0x52, 0x7a,
                                    0xfc, 0x36, 0xe1, 0xc4, 0x40, 0xc6, 0x65, 0x8e, 0x55, 0x5b, 0x54, 0x21,
                                    0x41, 0x1d, 0xe0, 0xe5, 0x0b, 0x4b, 0x62, 0xc5 },
        .result_size = 264, .result = { 0x04, 0x82, 0x01, 0x04,
                                        0xF2, 0x82, 0x8F, 0x71, 0x50, 0xDC, 0x2E, 0x7D, 0x1D, 0x9E, 0x1C, 0xEA,
                                        0xB4, 0x47, 0x2C, 0xC7, 0xC6, 0x11, 0x5D, 0x2D, 0xE6, 0xEC, 0x3D, 0xEA,
                                        0x24, 0x06, 0x6A, 0x37, 0xBC, 0x80, 0xDD, 0x8E, 0x9B, 0xF0, 0x5D, 0x5C,
                                        0x1A, 0xB4, 0x48, 0xEC, 0x23, 0x20, 0x98, 0x63, 0x87, 0x0E, 0x21, 0xA6,
                                        0x20, 0xEF, 0xD8, 0xDE, 0xD8, 0x89, 0xCA, 0x8C, 0x42, 0x3C, 0xCE, 0xDC,
                                        0xC3, 0x7F, 0xA0, 0xB2, 0xCE, 0xF6, 0xCC, 0x0B, 0x46, 0x5F, 0xC0, 0xD6,
                                        0x2A, 0x6E, 0x0D, 0x4E, 0x78, 0xA1, 0x4C, 0x17, 0xB6, 0xA7, 0x48, 0xCA,
                                        0x05, 0x54, 0xF1, 0xA1, 0x03, 0xAC, 0xB0, 0x2E, 0xA5, 0xDD, 0xF2, 0x6A,
                                        0x51, 0xE5, 0xA0, 0xCF, 0xD1, 0x94, 0xBC, 0x8C, 0x45, 0xA8, 0xA6, 0x41,
                                        0x71, 0xAF, 0x9C, 0x95, 0xB3, 0xB6, 0x71, 0x07, 0xDB, 0x95, 0xC9, 0x36,
                                        0x18, 0xBC, 0x61, 0x24, 0x33, 0x4C, 0x39, 0x04, 0x82, 0xCB, 0x1D, 0x09,
                                        0xF4, 0xB7, 0x2C, 0xE8, 0x23, 0x5E, 0x24, 0x99, 0x85, 0x0A, 0xC4, 0x2F,
                                        0xE7, 0xDC, 0xB4, 0x3C, 0xA8, 0xF3, 0xFB, 0xC6, 0x6E, 0xD8, 0xFD, 0x76,
                                        0xC2, 0x50, 0x15, 0xC9, 0x6D, 0x2F, 0xCF, 0x1B, 0x8F, 0x73, 0x24, 0x19,
                                        0x12, 0x21, 0xA1, 0x50, 0x37, 0x8F, 0xA4, 0x27, 0x6C, 0x0B, 0x39, 0xEF,
                                        0x24, 0xD8, 0x3D, 0x97, 0xC8, 0x46, 0x5D, 0x86, 0x0F, 0x99, 0x24, 0x48,
                                        0x94, 0xD0, 0x51, 0x49, 0x9A, 0xB2, 0x29, 0x51, 0x66, 0x41, 0xD0, 0x52,
                                        0xA3, 0x15, 0x5A, 0x99, 0xFB, 0xF2, 0x2D, 0xFC, 0x73, 0xA7, 0x03, 0xD5,
                                        0xB6, 0x45, 0x48, 0x11, 0x93, 0xA9, 0xB7, 0x2D, 0xD4, 0x22, 0xBB, 0x87,
                                        0xA3, 0xD4, 0x5D, 0xFF, 0xDA, 0x84, 0x91, 0x6C, 0xEA, 0x72, 0x52, 0x7A,
                                        0xFC, 0x36, 0xE1, 0xC4, 0x40, 0xC6, 0x65, 0x8E, 0x55, 0x5B, 0x54, 0x21,
                                        0x41, 0x1D, 0xE0, 0xE5, 0x0B, 0x4B, 0x62, 0xC5, }, },
};


#define kTestsPerTestCase 8
static void one_test(const struct test_case * thisCase)
{
    CFDataRef start = CFDataCreateWithBytesNoCopy(NULL, thisCase->data, thisCase->data_size, kCFAllocatorNull);

    uint8_t buffer[kMaxResultSize];
    uint8_t *buffer_end = buffer + sizeof(buffer);

    uint8_t* encoded = der_encode_plist(start, NULL, buffer, buffer_end);

    ok(encoded != NULL &&
       (thisCase->result_size == (buffer_end - encoded)) &&
       (memcmp(encoded, thisCase->result, thisCase->result_size) == 0));

    encoded = der_encode_data(start, NULL, buffer, buffer_end);

    ok(encoded != NULL &&
       (thisCase->result_size == (buffer_end - encoded)) &&
       (memcmp(encoded, thisCase->result, thisCase->result_size) == 0));

#if 0
    printf(".size = %d, .res = { ", (buffer_end - encoded));
    for(int c = 0; c < (buffer_end - encoded); ++c)
        printf("0x%02X, ", encoded[c]);
    printf("},\n");
#endif

    CFDataRef decoded = NULL;
    const uint8_t* decode_end = der_decode_data(NULL,
                                                &decoded, NULL, encoded, buffer_end);

    ok(decode_end == buffer_end);
    ok((decoded != NULL) && CFEqual(decoded, start));

    CFTypeRef decoded_type = NULL;
    decode_end = der_decode_plist(NULL,
                                  &decoded_type, NULL, encoded, buffer_end);

    ok(decode_end == buffer_end);
    ok((decoded != NULL) && CFEqual(decoded_type, start));

    ok(der_sizeof_data(start, NULL) == thisCase->result_size);
    ok(der_sizeof_plist(start, NULL) == thisCase->result_size);

    CFReleaseNull(start);
    CFReleaseNull(decoded);
    CFReleaseNull(decoded_type);
}

#define kTestCount (array_size(test_cases) * kTestsPerTestCase)
static void tests(void)
{
    for (int testnumber = 0; testnumber < array_size(test_cases); ++testnumber)
        one_test(test_cases + testnumber);
}

int su_11_cfdata_der(int argc, char *const *argv)
{
    plan_tests(kTestCount);
    tests();

    return 0;
}
